﻿(function ($) {

    // 颜色16进制验证   
    $.validator.addMethod("isColorStyle", function (value, element) {
        var tel = /^#[0-9,a-f,A-F]{6}$/;
        return this.optional(element) || (tel.test(value));
    }, "请正确填写颜色样式");


    var _projectService = abp.services.app.project;
    var _$modal = $('#EnvironmentEditModal');
    var _$form = $('form[name=EnvironmentEditForm]');

    _$form.validate({
        rules: {
            Order: {
                required: true,
                digits: true
            },
            Color: {
                isColorStyle:true
            }
        }
    });


    function save() {

        if (!_$form.valid()) {
            return;
        }

        var project = _$form.serializeFormToObject(); //serializeFormToObject is defined in main.js
        

        abp.ui.setBusy(_$form);
        _projectService.updateEnvironment(project).done(function () {
            _$modal.modal('hide');
            location.reload(true); //reload page to see edited user!
        }).always(function () {
            abp.ui.clearBusy(_$modal);
        });
    }

    //Handle save button click
    _$form.closest('div.modal-content').find(".save-button").click(function (e) {
        e.preventDefault();
        save();
    });

    //Handle enter key
    _$form.find('input').on('keypress', function (e) {
        if (e.which === 13) {
            e.preventDefault();
            save();
        }
    });

    $.AdminBSB.input.activate(_$form);

    _$modal.on('shown.bs.modal', function () {
        _$form.find('input[type=text]:first').focus();
    });
})(jQuery);